package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC74 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if (null == matrix || matrix.length <= 0) return false;
            int len = matrix.length * matrix[0].length - 1, row = matrix.length, col = matrix[0].length;
            int left = 0, right = len, mid;
            while (left <= right) {
                mid = left + (right - left) / 2;
                if (matrix[mid / col][mid % col] == target) return true;
                if (matrix[mid / col][mid % col] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();

        int[][] arr = InputUtil.toDoubleIntegerArray("[\n" +
                "  [1,   3,  5,  7],\n" +
                "  [10, 11, 16, 20],\n" +
                "  [23, 30, 34, 50]\n" +
                "]");
        System.out.println(solution.searchMatrix(arr, 50));
    }

    public static void main(String[] args) throws Exception {
        LC74 an = new LC74();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
